{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remaining_funds 36\n",
      "   weights  latest_price  n_shares  diff_weights\n",
      "0     0.56            11        50     -0.010539\n",
      "1     0.34            53         6      0.010124\n",
      "2     0.10            24         4      0.000415\n",
      "min_price 11\n",
      "row_idx_1 1\n",
      "row_idx_1 2\n",
      "row_idx_2 2\n",
      "   weights  latest_price  n_shares  diff_weights\n",
      "0     0.56            11        50     -0.010539\n",
      "1     0.34            53         6      0.010124\n",
      "2     0.10            24         4      0.000415\n",
      "row_idx_1 1\n",
      "row_idx_1 2\n",
      "row_idx_1 0\n",
      "row_idx_2 0\n",
      "   weights  latest_price  n_shares  diff_weights\n",
      "0     0.56            11        50     -0.010539\n",
      "1     0.34            53         6      0.010124\n",
      "2     0.10            24         4      0.000415\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>weights</th>\n",
       "      <th>latest_price</th>\n",
       "      <th>n_shares</th>\n",
       "      <th>diff_weights</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.56</td>\n",
       "      <td>11</td>\n",
       "      <td>50</td>\n",
       "      <td>-0.010539</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.34</td>\n",
       "      <td>53</td>\n",
       "      <td>6</td>\n",
       "      <td>0.010124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.10</td>\n",
       "      <td>24</td>\n",
       "      <td>4</td>\n",
       "      <td>0.000415</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   weights  latest_price  n_shares  diff_weights\n",
       "0     0.56            11        50     -0.010539\n",
       "1     0.34            53         6      0.010124\n",
       "2     0.10            24         4      0.000415"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'weights': [0.56, 0.34, 0.1],\n",
    "                  'latest_price': [11, 53, 24],})\n",
    "total_fund = 1000\n",
    "\n",
    "df['n_shares'] = (df['weights'] * total_fund/df['latest_price']).astype(int)\n",
    "df['diff_weights'] = df['weights'] - ((df['n_shares']*df['latest_price'])/sum(df['n_shares']*df['latest_price']))\n",
    "remaining_funds = total_fund - sum(df['n_shares']*df['latest_price'])\n",
    "print('remaining_funds', remaining_funds)\n",
    "print(df)\n",
    "print('min_price', min(df['latest_price']))\n",
    "\n",
    "while min(df['latest_price']) <= remaining_funds:\n",
    "    for i in range(len(df)):\n",
    "        row_value = np.sort(df['diff_weights'].values)[-(i+1)]\n",
    "        row_idx = df.index[df['diff_weights'] == row_value][0]\n",
    "        print('row_idx_1', row_idx)\n",
    "        if df.loc[df.index[row_idx], 'latest_price'] <= remaining_funds:\n",
    "            df.loc[df.index[row_idx], 'n_shares'] == df.loc[df.index[row_idx], 'n_shares']+1\n",
    "            remaining_funds -= df.loc[df.index[row_idx], 'latest_price']\n",
    "            print('row_idx_2', row_idx)\n",
    "            print(df)\n",
    "            break\n",
    "        else:\n",
    "            continue\n",
    "        \n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----\n",
      "0.01012448132780086\n",
      "-----\n",
      "0.000414937759336112\n",
      "-----\n",
      "-0.010539419087136848\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(df)):\n",
    "    row_idx = np.sort(df['diff_weights'].values)[-(i+1)]\n",
    "    print('-----')\n",
    "    print(row_idx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
